const Order = require('./backend/src/models/Order');

async function testOrderCreate() {
  console.log('Testing Order.create with different scenarios...');
  
  // Test 1: Member dine-in order (user_id = 1)
  const memberOrder = {
    user_id: 1,
    order_type: 'dine_in',
    subtotal: 100,
    tax_amount: 7,
    total_amount: 107,
    payment_method: 'cash'
  };
  
  const orderItems = [
    {
      menu_item_id: 1,
      quantity: 2,
      unit_price: 50,
      total_price: 100
    }
  ];
  
  try {
    console.log('Creating member dine-in order...');
    const result = await Order.create(memberOrder, orderItems);
    console.log('Created order:', result);
    
    // Fetch the created order to see the order_number
    const createdOrder = await Order.findById(result.orderId);
    console.log('Order details:', createdOrder);
    console.log('Order number:', createdOrder.order_number);
    
  } catch (error) {
    console.error('Error creating order:', error);
  }
  
  // Test 2: Guest takeaway order (user_id = null)
  const guestOrder = {
    user_id: null,
    order_type: 'takeaway',
    subtotal: 50,
    tax_amount: 3.5,
    total_amount: 53.5,
    payment_method: 'cash'
  };
  
  try {
    console.log('\nCreating guest takeaway order...');
    const result2 = await Order.create(guestOrder, orderItems);
    console.log('Created order:', result2);
    
    // Fetch the created order to see the order_number
    const createdOrder2 = await Order.findById(result2.orderId);
    console.log('Order details:', createdOrder2);
    console.log('Order number:', createdOrder2.order_number);
    
  } catch (error) {
    console.error('Error creating guest order:', error);
  }
}

testOrderCreate();